package com.titan.demo.camel.developers.SpringRoute.run;

import com.titan.demo.camel.developers.SpringRoute.domain.BodyDomain;
import com.titan.demo.camel.developers.SpringRoute.services.IAService;
import com.titan.demo.camel.developers.SpringRoute.services.IBService;
import com.titan.demo.camel.developers.SpringRoute.services.ICService;
import com.titan.demo.camel.developers.SpringRoute.services.IDService;
import com.titan.demo.camel.developers.SpringRoute.services.Utils;


/**
 * 模拟同步执行
 *
 * @author Administrator
 *
 */
public class JavaRunSync {
    public static void main(String[] args) {
        try {
            String[] paths = new String[]{   
                    "classpath:zzz/developers/SpringRoute/config/Services-Spring.xml" 
            };
            Utils.init(paths);

            BodyDomain body = new BodyDomain();
            body.append("start:");
            body.put("c01", true);
            body.put("c02", true);
            body.put("c03", true); 
            
            long time = System.currentTimeMillis();
            execute(body);
            time = System.currentTimeMillis() - time;
            System.out.println("执行用时："+time+"毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void execute(BodyDomain body) throws Exception {
        // 按照顺序执行，只允许串行
        IAService a = Utils.getBean("AService", IAService.class);
        a.a01(body);
        a.a02(body);
        a.a03(body);
        
        // 允许并行，但是必须等待执行结果
        IBService b = Utils.getBean("BService", IBService.class);
        b.b01(body);
        b.b02(body);
        b.b03(body);        
        
        // 根据选择执行，只允许串行
        ICService c = Utils.getBean("CService", ICService.class);
        if((Boolean)body.get("c01"))  c.c01(body);
        if((Boolean)body.get("c02"))  c.c02(body);
        if((Boolean)body.get("c03"))  c.c03(body);
        
        // 允许异步
        IDService d = Utils.getBean("DService", IDService.class);
        d.d01(body);
        d.d02(body);
        d.d03(body);        
    }
}

